package nine.test_9_7;

import java.util.Scanner;

public class TestMain {
    static boolean[] bs;
    static int k = 0;
    static int max = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[][] arr = new int[k][k];
        bs = new boolean[n];
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < k; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }
        scanner.nextLine();
        String str = scanner.nextLine();
        dfs(arr,str,n,0);
        System.out.println(max);
    }
    private static void dfs(int[][] arr,String str,int n,int count) {
        if(n == k) {
            max = Math.max(max,count);
            return;
        }
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if(!bs[i] && !bs[j]) {
                    int x = str.charAt(i) - 'a';
                    int y = str.charAt(j) - 'a';
                    count += arr[x][y];
                    bs[i] = true;
                    bs[j] = true;
                    k += 2;
                    dfs(arr,str,n,count);
                    count -= arr[x][y];
                    k -= 2;
                    bs[i] = false;
                    bs[j] = false;
                }
            }
        }
    }
}
